import React, { useRef } from 'react';

import { Button, Space, Modal } from 'dw-mx';
import { Dialog } from 'dw-mx-extend';
import { openWindowByPost, request } from 'dw-mx-request';

import UploadDemo from './UploadDemo';
import CurrentUser from '@/auth/CurrentUser';

import moment from 'moment';

export default function Utils() {
    const dialogRef = useRef<Dialog>();

    ///// Sef1
    const sef1Login = async () => {
        const token = await request('/sef1/mx/test/doLogin', {
            username: 'admin',
            password: '123456',
        });

        alert(token);

        CurrentUser.logIn('admin', 'admin', token);
    };
    const sef1GetUser = async () => {
        const userName = await request('/sef1/mx/test/getUser');

        alert(userName);
    };
    const sef1DataDo = async () => {
        const ret = await request('/sef1/mx/test/dataobject');
        console.log(ret);
        alert(ret.xm);
    };
    const sef1DataString = async () => {
        const ret = await request('/sef1/mx/test/string');
        alert(ret);
    };
    const sef1DataVoid = async () => {
        const ret = await request('/sef1/mx/test/void');
        alert(ret);
    };
    const sef1DataHashMap = async () => {
        const ret = await request('/sef1/mx/test/hashmap');
        console.log(ret);
        alert(JSON.stringify(ret));
    };
    const sef1ESAPI = async () => {
        const ret = await request('/sef1/mx/test/esapi');
        alert(JSON.stringify(ret));
    };
    const sef1ESAPIHashMap = async () => {
        const ret = await request('/sef1/mx/test/esapihashmap');
        alert(JSON.stringify(ret));
    };

    const sef1ExceptionBiz = async () => {
        const ret = await request('/sef1/mx/test/businessException');
        alert(ret);
    };
    const sef1ExceptionApp = async () => {
        const ret = await request('/sef1/mx/test/appException');
        alert(ret);
    };
    const sef1ExceptionSession = async () => {
        const ret = await request('/sef1/mx/test/sessionOrUserNotExistException');
        alert(ret);
    };

    const sef1Download = () => {
        request.download('/sef1/mx/test/file', {
            fileName: 'MouseX测试文件名.txt',
        });
    };
    const sef1DownloadError = () => {
        request.download('/sef1/mx/test/fileerror', {
            fileName: 'MouseX测试文件名.txt',
        });
    };
    const sef1Upload = async () => {
        dialogRef.current.openDialog(UploadDemo, {
            title: '文件上传',
            props: {
                target: 'sef1',
            },
        });
    };
    const sef1Delay = async () => {
        await request('/sef1/mx/test/delay');
    };
    const sef1404 = async () => {
        await request('/sef1/mx/test/path/not/exist');
    };
    const sef1SendData = async () => {
        await request('/sef1/mx/test/receive', {
            moment: moment(),
            date: new Date(),
            string: '你好WEBJ2EE',
            number: 123.0,
            boolean: true,
        });
    };

    ///// Sef2
    const sef2Login = async () => {
        const token = await request('/sef2/mx/test/doLogin', {
            username: 'admin',
            password: '123456',
        });

        alert(token);

        CurrentUser.logIn('admin', 'admin', token);
    };
    const sef2DataDo = async () => {
        const ret = await request('/sef2/mx/test/dataobject');
        console.log(ret);
        alert(ret.xm);
    };
    const sef2DataString = async () => {
        const ret = await request('/sef2/mx/test/string');
        alert(ret);
    };
    const sef2DataVoid = async () => {
        const ret = await request('/sef2/mx/test/void');
        alert(ret);
    };
    const sef2DataHashMap = async () => {
        const ret = await request('/sef2/mx/test/hashmap');
        console.log(ret);
        alert(JSON.stringify(ret));
    };
    const sef2ExceptionBiz = async () => {
        const ret = await request('/sef2/mx/test/businessException');
        alert(ret);
    };
    const sef2ExceptionApp = async () => {
        const ret = await request('/sef2/mx/test/appException');
        alert(ret);
    };
    const sef2ExceptionSession = async () => {
        const ret = await request('/sef2/mx/test/sessionOrUserNotExistException');
        alert(ret);
    };

    const sef2Download = () => {
        request.download('/sef2/mx/test/file', {
            fileName: 'MouseX测试文件名.txt',
        });
    };
    const sef2DownloadError = () => {
        request.download('/sef2/mx/test/fileerror', {
            fileName: 'MouseX测试文件名.txt',
        });
    };
    const sef2Upload = async () => {
        dialogRef.current.openDialog(UploadDemo, {
            title: '文件上传',
            props: {
                target: 'sef1',
            },
        });
    };
    const openNewWindowDemo = () => {
        openWindowByPost('http://10.1.60.10:8810/sep');
    };

    ///// Leaf6
    const leaf6Login = async () => {
        const ret = await request('/leaf6/zuul/api/auth/password/login', {
            username: 'admin',
            password: '123456',
            captcha: 'rp9c',
        });

        const token = ret['Access-Token'];

        alert(token);

        CurrentUser.logIn('admin', 'admin', token);
    };

    const leaf6Biz = async () => {
        const ret = await request('/leaf6/zuul/rsrcd/rsrc/UTCDemo/getAllList', {
            username: 'admin',
            password: '123456',
            captcha: 'rp9c',
        });
        alert(JSON.stringify(ret));
    };

    ///// mock
    const mockLogin = async () => {
        const ret = await request('/mock/login/doLogin', {
            username: 'admin',
            password: '123456',
            captcha: 'rp9c',
        });
        alert(ret);
    };
    return (
        <>
            <div>
                <h2>SEF1</h2>
                <Space>
                    <Button onClick={sef1Login}>登录请求</Button>
                    <Button onClick={sef1GetUser}>取用户</Button>
                    <Button onClick={sef1DataDo}>数据-DataObject</Button>
                    <Button onClick={sef1DataString}>数据-String</Button>
                    <Button onClick={sef1DataVoid}>数据-void</Button>
                    <Button onClick={sef1DataHashMap}>数据-hashmap</Button>
                    <Button onClick={sef1ESAPI}>数据-ESAPI</Button>
                    <Button onClick={sef1ESAPIHashMap}>数据-ESAPI-hashmap</Button>
                    <Button onClick={sef1ExceptionBiz}>异常-Biz</Button>
                    <Button onClick={sef1ExceptionApp}>异常-App</Button>
                    <Button onClick={sef1ExceptionSession}>异常-Session</Button>
                    <Button onClick={sef1Download}>下载</Button>
                    <Button onClick={sef1DownloadError}>下载-Error</Button>
                    <Button onClick={sef1Upload}>上传</Button>
                    <Button onClick={sef1Delay}>延时(5s)</Button>
                    <Button onClick={sef1SendData}>发送数据</Button>
                    <Button onClick={sef1404}>404</Button>
                    <Dialog ref={dialogRef} />
                </Space>
            </div>
            <div>
                <h2>SEF2</h2>
                <Space>
                    <Button onClick={sef2Login}>登录请求</Button>
                    <Button onClick={sef2DataDo}>数据-DataObject</Button>
                    <Button onClick={sef2DataString}>数据-String</Button>
                    <Button onClick={sef2DataVoid}>数据-void</Button>
                    <Button onClick={sef2DataHashMap}>数据-hashmap</Button>
                    <Button onClick={sef2ExceptionBiz}>异常-Biz</Button>
                    <Button onClick={sef2ExceptionApp}>异常-App</Button>
                    <Button onClick={sef2ExceptionSession}>异常-Session</Button>
                    <Button onClick={sef2Download}>下载</Button>
                    <Button onClick={sef2DownloadError}>下载-Error</Button>
                    <Button onClick={sef2Upload}>上传</Button>
                    <Button onClick={openNewWindowDemo}>POST-打开新窗口</Button>
                </Space>
            </div>
            <div>
                <h2>LEAF6</h2>
                <Space>
                    <Button onClick={leaf6Login}>登录请求</Button>
                    <Button onClick={leaf6Biz}>业务请求</Button>
                </Space>
            </div>
            <div>
                <h2>Mock</h2>
                <Space>
                    <Button onClick={mockLogin}>登录请求</Button>
                </Space>
            </div>
        </>
    );
}
